<?php 
include('header.php');
$n1 = rand(1, 9);
$n2 = rand(1, 9);

if($site['reg_status'] == 0){
if(isset($_POST['register'])){
$name = $db->EscapeString($_POST['user']);
$email = $db->EscapeString($_POST['email']);
$pass1 = $db->EscapeString($_POST['password']);
$pass2 = $db->EscapeString($_POST['password2']);
$nr1 = base64_decode($_POST['nr1']);
$nr2 = base64_decode($_POST['nr2']);

if($nr1 + $nr2 != $_POST['captcha']){
	$mesaj = '<div class="error">ERROR: Security answer is wrong!</div>';
}elseif ($db->GetNumRows($db->Query("SELECT id FROM `users` WHERE `login`='".$name."' OR `email`='".$email."'")) > 0) {
	$mesaj = '<div class="error">ERROR: Username or email already registered!</div>';
}elseif (!isUserID($name)) {
	$mesaj = '<div class="error">ERROR: Username is incorrect!</div>';
}elseif(!isEmail($email)) {
	$mesaj = '<div class="error">ERROR: Enter a valid email address!</div>';
}elseif (!checkPwd($pass1,$pass2)) {
	$mesaj = '<div class="error">ERROR: Passwords do not match!</div>';
}else{
	if(isset($_COOKIE['PlusREF'])){
		$ref = $db->EscapeString($_COOKIE['PlusREF']);
		$sql = $db->Query("SELECT id,login FROM `users` WHERE `id`='".$ref."'");
		$user = $db->FetchArray($sql);
		$db->Query("INSERT INTO `referals`(user,referal,date) values('".$user['login']."','".$name."',NOW())");
	}

	if($site['reg_reqmail'] == 0){
		$activate = rand(100000000, 999999909);
		mail($email,"Activation","
Hello!

Thank you for joining {$site['site_name']}, a free Social Media Exchange. 

Please click the link below to activate your account: 
{$site['site_url']}/activate.php?cod={$activate}
	
With Regards!","From: {$site['site_email']} <{$site['site_email']}>");
	}else{
		if($user['id'] > 0 && $user['id'] != ''){
			$db->Query("UPDATE `users` SET `coins`=`coins`+'".$site['ref_coins']."' WHERE `id`='".$user['id']."'");
		}
		$activate = 0;
	}

	$final = VisitorIP();
	$passc = MD5($pass1);
	
	$db->Query("INSERT INTO `users`(email,login,coins,IP,pass,ref,signup,activate) values('".$email."','".$name."','".$site['reg_coins']."','".$final."','".$passc."','".$user['id']."',NOW(),'".$activate."')");
	$mesaj = '<div class="success">Success! '.($site['reg_reqmail'] == 0 ? 'You need to confirm your email address now!' : 'Now you can login on site!').'</div>';
	}
}
?>	
<div class="block medium right">
			<div class="top">
                  <h1>Register</h1>
            </div>
			<div class="content"><div class="msg"><?=$mesaj?></div>
				<form action="" method="post">
					<input type="hidden" name="nr1" value="<?=base64_encode($n1)?>" />
					<input type="hidden" name="nr2" value="<?=base64_encode($n2)?>" />
					  <fieldset>
						 <p>
							 <label>Username</label><br/>
							 <input class="text big" type="text" value="<?=$_POST['user']?>" name="user" required="required" />
						 </p>
						 <p>
							 <label>Email</label><br/>
							 <input class="text big" type="email" value="" name="email" required="required" />
						 </p>
						 <p>
							 <label>Password</label> <br/>
							 <input class="password" type="password" value="" name="password" required="required" />
						 </p>
						 <p>
							 <label>Repeat Password</label> <br/>
							 <input class="password" type="password" value="" name="password2" required="required" />
						 </p>
						 <p>
							 <label><?=($n1." + ".$n2." = ?")?></label> <small class="above">Enter your answer</small><br/>
							 <input class="text big" type="text" value="" name="captcha" required="required" />
						 </p>
						  <p style="text-align: center; padding-top: 15px;">
						  	<input type="submit" class="gbutton" value="Register" name="register" />
						  </p>
					  </fieldset>
				</form>
            </div>
		</div>
<?}else{?>
<div class="block medium right">
			<div class="top">
                  <h1>Register</h1>
            </div>
			<div class="content"><div class="msg"><div class="error">Registration is currently disabled!</div></div>
			</div>
</div>
<?}
include('footer.php');?>